package com.kbcss.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class InsertDis
 */
public class InsertDis extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertDis() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		
		int xx=0;
		//HttpSession ss=request.getSession();
		String uid=request.getParameter("q");
		String topic=request.getParameter("p");
		String des=request.getParameter("r");
		String dept=request.getParameter("s");
		//String dept=(String)ss.getAttribute("dept");
		java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
	       Calendar calendar = new GregorianCalendar();
	       String am_pm;
	       int hour = calendar.get(Calendar.HOUR);
	       int minute = calendar.get(Calendar.MINUTE);
	       int second = calendar.get(Calendar.SECOND);
	       if(calendar.get(Calendar.AM_PM) == 0)
	      am_pm = "AM";
	      else
	      am_pm = "PM";
	       String date=sqlDate.toString();
	       String time=hour + ":" + minute + ":" + second + " " + am_pm;
		
		
		try{
		     Class.forName("com.mysql.jdbc.Driver");
		     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kbcssdb", "root", "mysql");
		     PreparedStatement ps=con.prepareStatement("insert into discussion(userid,deptid,distopic,disdate,distime,disdesc) values(?,?,?,?,?,?)");
		     ps.setString(1,uid);
		     ps.setString(2,dept);
		     ps.setString(3,topic);
		     ps.setString(4,date);
		     ps.setString(5,time);
		     ps.setString(6,des);
		     xx=ps.executeUpdate();
	            if(xx==1){
	                         //con.commit();
	                         out.println(xx);
	                     }
	}
	catch(Exception ex){
		out.println("error...."+ex.getMessage());
	}
		
	}

}
